package com.yanqu.road.server.manager.tomb.dao;

import com.fasterxml.jackson.core.type.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.dbV2.serializer.Helper;
import com.yanqu.road.dao.impl.TempDataObjectDao;

import com.yanqu.road.server.manager.tomb.entity.DbTombPosition;
import com.yanqu.road.server.manager.tomb.entity.DigUserInfo;
import com.yanqu.road.server.manager.tomb.entity.PositionInfo;

import java.sql.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class TombPositionDaoImpl extends TempDataObjectDao<DbTombPosition> {


    public static TombPositionDaoImpl newInstance() {
        TombPositionDaoImpl dao = new TombPositionDaoImpl();
        return dao;
    }

    public static String updateSql = "update `t_c_activity_tomb_position` set done=?,pEnum=?,finishUserId=?,eventId=?,eType=?,maxHp=?,currentHp=?,overTime=?,eventInfoMap=?,pvpUserId=?,helpUserIdList=?,isSendHelp=?,digUnionUserMap=?,status=?,updateTime=? where activityId=? and groupId=? and floor=? and x=? and y=?";

    @Override
    protected boolean updateData(DbTombPosition data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<Integer, DbParameter>();

        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getDone()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getpEnum()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getFinishUserId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getEventId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.geteType()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getMaxHp()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getCurrentHp()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getOverTime()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, Helper.toJSONString(data.getEventInfoMap())));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getPvpUserId()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, Helper.toJSONString(data.getHelpUserIdList())));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getIsSendHelp()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, Helper.toJSONString(data.getDigUnionUserMap())));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getStatus()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));

        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getFloor()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getX()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getY()));
        if (getDb().executeNoneQuery(updateSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSql = "insert into `t_c_activity_tomb_position`(activityId,groupId,done,floor,x,y,pEnum,finishUserId,eventId,eType,maxHp,currentHp,overTime,eventInfoMap,pvpUserId,helpUserIdList,isSendHelp,digUnionUserMap,status,createTime,updateTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DbTombPosition data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<Integer, DbParameter>();
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getDone()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getFloor()));

        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getX()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getY()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getpEnum()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getFinishUserId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getEventId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.geteType()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getMaxHp()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getCurrentHp()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getOverTime()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, Helper.toJSONString(data.getEventInfoMap())));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getPvpUserId()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, Helper.toJSONString(data.getHelpUserIdList())));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getIsSendHelp()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, Helper.toJSONString(data.getDigUnionUserMap())));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getStatus()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        if (getDb().executeNoneQuery(insertSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }


    @Override
    protected DbTombPosition parseData(ResultSet rs) throws SQLException {
        DbTombPosition data = DbTombPosition.newInstance();
        data.setActivityId(rs.getInt("activityId"));
        data.setGroupId(rs.getInt("groupId"));
        data.setDone(rs.getInt("done"));
        data.setFloor(rs.getInt("floor"));
        data.setX(rs.getInt("x"));
        data.setY(rs.getInt("y"));
        data.setpEnum(rs.getInt("pEnum"));
        data.setFinishUserId(rs.getLong("finishUserId"));
        data.setEventId(rs.getInt("eventId"));
        data.seteType(rs.getInt("eType"));
        data.setMaxHp(rs.getLong("maxHp"));
        data.setCurrentHp(rs.getLong("currentHp"));
        data.setOverTime(rs.getLong("overTime"));
        data.setEventInfoMap(Helper.toObject(rs.getString("eventInfoMap"), new TypeReference<ConcurrentHashMap<Long, PositionInfo>>() {
        }));
        if (data.getEventInfoMap() == null) {
            data.setEventInfoMap(new ConcurrentHashMap<>());
        }
        data.setPvpUserId(rs.getLong("pvpUserId"));
        data.setHelpUserIdList(Helper.toObject(rs.getString("helpUserIdList"), new TypeReference<HashSet<Long>>() {
        }));
        if (data.getHelpUserIdList() == null) {
            data.setHelpUserIdList(new HashSet<>());
        }
        data.setIsSendHelp(rs.getInt("isSendHelp"));
        data.setDigUnionUserMap(Helper.toObject(rs.getString("digUnionUserMap"), new TypeReference<ConcurrentHashMap<String, DigUserInfo>>() {
        }));
        if (data.getDigUnionUserMap() == null) {
            data.setDigUnionUserMap(new ConcurrentHashMap<>());
        }
        data.setStatus(rs.getInt("status"));

        data.setCreateTime(rs.getLong("createTime"));
        data.setUpdateTime(rs.getLong("updateTime"));

        return data;
    }

    public DbTombPosition getData(int activityId, int groupId, int floor) {
        ResultSet rs = null;
        CallableStatement call = null;
        String selectSql = "select * from t_c_activity_tomb_position where activityId = ? and groupId=? and floor = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setInt(1, activityId);
            call.setInt(2, groupId);
            call.setInt(3, floor);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public Map<String, DbTombPosition> getDataMap(int activityId, int done) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, DbTombPosition> dataMap = new ConcurrentHashMap<>();
        String selectSql = "select * from t_c_activity_tomb_position where activityId = ? and done = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setInt(1, activityId);
            call.setInt(2, done);
            rs = call.executeQuery();
            while (rs.next()) {
                DbTombPosition data = parse(rs);
                dataMap.put(data.getKey(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public boolean saveData(DbTombPosition data) {
        if (data.isUpdateOption()) {
            if (data.isNeedInsert()) {
                if (addData(data)) {
                    data.setNeedInsert(false);
                }
            } else {
                data.setUpdateTime(System.currentTimeMillis());
                update(data);
            }
        }
        return true;
    }

}
